home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0113_String Concatenation Functions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  5.1 KB  |  128 lines

  1. {From:         Joel Lichtenwalner <JWillard44@AOL.COM>}
  2.  
  3. PROGRAM TEST_CONCAT;  { TESTS VARIOUS METHODS OF CONCATENATING STRINGS }
  4.  
  5.   USES
  6.     CRT;
  7.  
  8.   CONST
  9.     ADD_STR     : STRING[22] = 'Test string 1234567890';
  10.     TICKS       = 90;           { LOOP FOR APPROX 5 SECONDS }
  11.  
  12.   VAR
  13.  
  14.     CLOCK_TICKS : WORD ABSOLUTE $40:$6C;  { Updated every 58 ms by system }
  15.     TIME_SAVE   : WORD;         { Variable used to keep track of time     }
  16.     LOOPS       : LONGINT;      { Loop control counter                    }
  17.     TARG        : STRING[255];  { Target string, to be added too          }
  18.     COMPUTE     : EXTENDED;
  19.  
  20.  
  21.   function strcont(s1,s2:string):string; assembler;
  22.     asm
  23.       push ds
  24.       cld
  25.       lds si,s1         {Load addresses of s1}
  26.       les di,s2         {Load addresses of s2}
  27.       xor ah,ah         {Clear ah & bh}
  28.       xor bh,bh         {     ""      }
  29.       mov al,ds:[si]    {Get the length of first string, copy into al}
  30.       mov bl,es:[di]    {Get the length of second string, copy into bl}
  31.       add ax,bx         {Add length of s1 to length s2}
  32.       cmp ax,255        {Compare}
  33.       ja @toolarge      {Jump to @toolarge if length(s1)+length(s2)>255}
  34.       les di,@result    {Copy location of @result into es:di}
  35.       mov cl,1          {Make sure at least one byte of beginning string}
  36.       xor ch,ch         {is transferred to @result.}
  37.       add cl,ds:[si]    {Add length of string to cl.}
  38.       rep movsb         {Copy first string into @result}
  39.       lds si,s2         {Get address of second string}
  40.       mov cl,ds:[si]    {Get length of second string, copy into cl}
  41.       cmp cl,0          {If second string is blank, skip adding it.}
  42.       je @end           {Jump to end if length of second string is zero.}
  43.       inc si            {Move pointer (si) to start of second string}
  44.       mov al,cl         {Save length of second string in al}
  45.       rep movsb         {Copy second string into @result}
  46.       lds si,@result    {Get location of @result}
  47.       add ds:[si],al    {Add lengths together}
  48.       jmp @end          {Skip to end}
  49.      @toolarge:         {If added strings total larger than 255, this sub}
  50.       les di,@result    {is called.}
  51.       xor al,al         {Make sure al is a zero.}
  52.       mov es:[di],al    {Move a "0" into the beginning of @result, making it}
  53.      @end:              {a null string.}
  54.       pop ds            {Return DS to normal so Pascal doesn't screw up.}
  55.       end;
  56.  
  57.   PROCEDURE ATTACH(VAR DEST,SOURCE:STRING);
  58.  
  59.     VAR
  60.         DESTL   : BYTE ABSOLUTE DEST;
  61.         SOURCEL : BYTE ABSOLUTE SOURCE;
  62.       BEGIN
  63.         MOVE(SOURCE[1],DEST[SUCC(DESTL)],SOURCEL);
  64.         INC(DESTL,SOURCEL);
  65.         END;
  66.     BEGIN
  67.       CLRSCR;  WRITELN('Test concatenation functions');
  68.       { -------- FIRST TEST -------- }
  69.       WRITE('Testing Pascal''s "+" function');
  70.       LOOPS := 0;
  71.       TIME_SAVE := CLOCK_TICKS;    { WAIT UNTIL THE CLOCK TURNS OVER }
  72.       REPEAT UNTIL CLOCK_TICKS <> TIME_SAVE;
  73.       TIME_SAVE := CLOCK_TICKS + TICKS;  { Set loop time }
  74.         REPEAT
  75.         TARG := '';
  76.         TARG := TARG + ADD_STR;  {  22 }
  77.         TARG := TARG + ADD_STR;  {  44 }
  78.         TARG := TARG + ADD_STR;  {  66 }
  79.         TARG := TARG + ADD_STR;  {  88 }
  80.         TARG := TARG + ADD_STR;  { 110 }
  81.         TARG := TARG + TARG;     { 220 }
  82.         TARG := TARG + ADD_STR;  { 242 }
  83.         INC(LOOPS);
  84.         UNTIL CLOCK_TICKS = TIME_SAVE;
  85.       COMPUTE := LOOPS; COMPUTE := COMPUTE / TICKS;
  86.       WRITELN(' Performance = ',COMPUTE:0:6,' Loops per tick');
  87.       { -------- SECOND TEST -------- }
  88.       WRITE('Testing STRCONT function');
  89.       LOOPS := 0;
  90.       TIME_SAVE := CLOCK_TICKS;    { WAIT UNTIL THE CLOCK TURNS OVER }
  91.       REPEAT UNTIL CLOCK_TICKS <> TIME_SAVE;
  92.       TIME_SAVE := CLOCK_TICKS + TICKS;  { Set loop time }
  93.         REPEAT
  94.         TARG := '';
  95.         TARG := strcont(TARG,ADD_STR);  {  22 }
  96.         TARG := strcont(TARG,ADD_STR);  {  44 }
  97.         TARG := strcont(TARG,ADD_STR);  {  66 }
  98.         TARG := strcont(TARG,ADD_STR);  {  88 }
  99.         TARG := strcont(TARG,ADD_STR);  { 110 }
  100.         TARG := strcont(TARG,TARG);     { 220 }
  101.         TARG := strcont(TARG,ADD_STR);  { 242 }
  102.         INC(LOOPS);
  103.         UNTIL CLOCK_TICKS = TIME_SAVE;
  104.       COMPUTE := LOOPS; COMPUTE := COMPUTE / TICKS;
  105.       WRITELN(' Performance = ',COMPUTE:0:6,' Loops per tick');
  106.       { -------- THIRD TEST -------- }
  107.       WRITE('Testing ATTACH procedure ');
  108.       LOOPS := 0;
  109.       TIME_SAVE := CLOCK_TICKS;    { WAIT UNTIL THE CLOCK TURNS OVER }
  110.       REPEAT UNTIL CLOCK_TICKS <> TIME_SAVE;
  111.       TIME_SAVE := CLOCK_TICKS + TICKS;  { Set loop time }
  112.         REPEAT
  113.         TARG := '';
  114.         ATTACH(TARG,ADD_STR);  {  22 }
  115.         ATTACH(TARG,ADD_STR);  {  44 }
  116.         ATTACH(TARG,ADD_STR);  {  66 }
  117.         ATTACH(TARG,ADD_STR);  {  88 }
  118.         ATTACH(TARG,ADD_STR);  { 110 }
  119.         ATTACH(TARG,TARG);     { 220 }
  120.         ATTACH(TARG,ADD_STR);  { 242 }
  121.         INC(LOOPS);
  122.         UNTIL CLOCK_TICKS = TIME_SAVE;
  123.       COMPUTE := LOOPS; COMPUTE := COMPUTE / TICKS;
  124.       WRITELN(' Performance = ',COMPUTE:0:6,' Loops per tick');
  125.       READLN;
  126.       END.
  127.  
  128.